﻿using liaowa2.Entity.DataModel;
using liaowa2.Entity.DTO;
using liaowa2.Impl.Manager;
using liaowa2.Interface.Manager;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web;
using System.Web.Mvc;

namespace liaowa2.Web.MenuHelper
{
    public class HtmlHelp
    {
        private static INavigationInterface navigationAppService = new NavigationImpl();
        private static IManagerRoleInterface managerRoleAppService = new ManagerRoleImpl();
        
        //public HtmlHelp()
        //{
        //    navigationAppService = new NavigationImpl();
        //}
        #region 导航
        public static MvcHtmlString NavList()
        {

            List<Navigation> list = navigationAppService.GetList("");
            StringBuilder sb = new StringBuilder();
            string tree = NavTree(list, 0, sb);
            return MvcHtmlString.Create(tree);
        }

        private static string NavTree(List<Navigation> list, int parent_id, StringBuilder sb)
        {
            List<Navigation> newList = list.FindAll(c => c.parent_id == parent_id);

            for (int i = 0; i < newList.Count; i++)
            {
                sb.Append("<tr>");

                sb.Append("<td align=\"center\">");
                sb.Append("<span class=\"aspNetDisabled checkall\" style=\"vertical-align:middle;\">");
                sb.Append("<input id=\"rptList\" type=\"checkbox\" name=\"rptList\" value=\"" + newList[i].id + "\">");
                sb.Append("</span>");

                sb.Append("</td>");

                sb.Append("<td></td>");

                sb.Append("<td style=\"white-space:nowrap;word-break:break-all;overflow:hidden;\">" + newList[i].name + "</td>");

                sb.Append("<td style=\"white-space:nowrap;word-break:break-all;overflow:hidden;\">");
                if (newList[i].parent_id != 0)
                {
                    if (newList[i].class_layer == 2)
                    {
                        sb.Append("<span style=\"display:inline-block;width:24px;\"></span>");
                    }
                    else if (newList[i].class_layer > 2)
                    {
                        sb.Append("<span style=\"display:inline-block;width:48px;\"></span>");
                    }
                    else
                    {
                        sb.Append("<span style=\"display:inline-block;width:0px;\"></span>");
                    }
                    sb.Append("<span class=\"folder-line\"></span>");
                }
                sb.Append("<span class=\"folder-open\"></span>");
                sb.Append("<a href=\"/nav/edit?id=" + newList[i].id + "\">" + newList[i].title + "</a>");
                if (!string.IsNullOrEmpty(newList[i].link_url))
                {
                    sb.Append("(链接：" + newList[i].link_url + ")");
                }
                sb.Append("</td>");

                sb.Append("<td align=\"center\">" + (newList[i].is_lock == 0 ? "√" : "×") + "</td>");

                sb.Append("<td>" + newList[i].sort_id + "</td>");

                sb.Append("<td align=\"center\" style=\"white-space:nowrap;word-break:break-all;overflow:hidden;\">");
                if (newList[i].class_layer < 3)
                {
                    sb.Append("<a href=\"/Nav/Edit?parent_id=" + newList[i].id + "\">添加子级</a>　");
                }
                sb.Append("<a href=\"/Nav/Edit?id=" + newList[i].id + "\">修改</a>");
                sb.Append("</td>");

                sb.Append("</tr>");
                NavTree(list, newList[i].id, sb);
            }
            return sb.ToString();
        }

        public static List<SelectListItem> NavDropList(string panretid, string id)
        {
            List<Navigation> list = navigationAppService.GetList("");
            List<SelectListItem> sellist = new List<SelectListItem>();
            if (Convert.ToInt16(id) > 0)
            {
                Navigation model = list.Find(c => c.id == Convert.ToInt16(id));
                if (model != null)
                {
                    sellist.Add(new SelectListItem() { Text = model.title, Value = model.id.ToString() });
                }
                return sellist;
            }
            List<SelectListItem> newlist = NavDropTree(sellist, list, 0, panretid);
            newlist.Insert(0, new SelectListItem() { Text = "无父级导航", Value = "0" });
            return newlist;
        }

        private static List<SelectListItem> NavDropTree(List<SelectListItem> selList, List<Navigation> list, int parentid, string panretid)
        {
            List<Navigation> newList = list.FindAll(c => c.parent_id == parentid && c.class_layer < 3);
            for (int i = 0; i < newList.Count; i++)
            {
                SelectListItem item = new SelectListItem();
                if (newList[i].parent_id > 0)
                {
                    if (newList[i].class_layer == 2)
                    {
                        item.Text = "　├" + newList[i].title;
                    }
                    if (newList[i].class_layer > 2)
                    {
                        item.Text = "　　├" + newList[i].title;
                    }
                }
                else
                {
                    item.Text = newList[i].title;
                }
                item.Value = newList[i].id.ToString();
                if (item.Value == panretid)
                {
                    item.Selected = true;
                }
                selList.Add(item);
                NavDropTree(selList, list, newList[i].id, panretid);
            }
            return selList;
        }
        #endregion

        #region 权限
        public static MvcHtmlString ManagerRoleList(int? id)
        {
            List<Navigation> list = navigationAppService.GetList("");
            StringBuilder sb = new StringBuilder();
            Entity.DTO.ManagerRoleDTO roleDTO = new Entity.DTO.ManagerRoleDTO();
            if (id.HasValue)
            {
                roleDTO = managerRoleAppService.GetRoleDTO(Convert.ToInt32(id));
            }
            string tree = ManagerRoleTree(list, 0, sb, roleDTO);
            return MvcHtmlString.Create(tree);
        }
        private static string ManagerRoleTree(List<Navigation> list, int parent_id, StringBuilder sb, ManagerRoleDTO roleDTO)
        {
            List<Navigation> newList = list.FindAll(c => c.parent_id == parent_id);
            for (int i = 0; i < newList.Count; i++)
            {
                sb.Append("<tr>");
                sb.Append("<td style=\"white-space:nowrap;word-break:break-all;overflow:hidden;\">");
                if (newList[i].class_layer == 1)
                {
                    sb.Append("<span class=\"folder-open\"></span>");
                }
                if (newList[i].class_layer == 2)
                {
                    sb.Append("<span style=\"display:inline-block;width:24px;\"></span><span class=\"folder-line\"></span><span class=\"folder-open\"></span>");
                }
                if (newList[i].class_layer > 2)
                {
                    sb.Append("<span style=\"display:inline-block;width:48px;\"></span><span class=\"folder-line\"></span><span class=\"folder-open\"></span>");
                }
                sb.Append(newList[i].title);
                sb.Append("</td>");
                sb.Append("<td>");
                string[] actionType = newList[i].action_type.Split(',');
                for (int j = 0; j < actionType.Length; j++)
                {
                    sb.Append("<span id=\"rptList_cblActionType_" + j + "\" class=\"cbllist\">");
                    if (roleDTO != null)
                    {
                        if (roleDTO.manager_role_values.Count > 0)
                        {
                            if (roleDTO.manager_role_values.FindAll(c => c.action_type == actionType[j] && c.nav_name == newList[i].name).ToList().Count > 0)
                            {
                                sb.Append("<input id=\"rptList_cblActionType\" checked type=\"checkbox\" name=\"rptList_cblActionType\" value=\"" + actionType[j] + "$" + newList[i].name + "\">");
                            }
                            else
                            {
                                sb.Append("<input id=\"rptList_cblActionType\" type=\"checkbox\" name=\"rptList_cblActionType\" value=\"" + actionType[j] + "$" + newList[i].name + "\">");
                            }
                        }
                        else
                        {
                            sb.Append("<input id=\"rptList_cblActionType\" type=\"checkbox\" name=\"rptList_cblActionType\" value=\"" + actionType[j] + "$" + newList[i].name + "\">");
                        }
                    }
                    else
                    {
                        sb.Append("<input id=\"rptList_cblActionType\" type=\"checkbox\" name=\"rptList_cblActionType\" value=\"" + actionType[j] + "$" + newList[i].name + "\">");
                    }
                    sb.Append("<label for=\"rptList_cblActionType_0_0_0\">");
                    switch (actionType[j])
                    {
                        case "Show":
                            sb.Append("显示");
                            break;
                        case "View":
                            sb.Append("查看");
                            break;
                        case "Add":
                            sb.Append("添加");
                            break;
                        case "Edit":
                            sb.Append("修改");
                            break;
                        case "Delete":
                            sb.Append("删除");
                            break;
                        default:
                            break;
                    }
                    sb.Append("</label>");
                    sb.Append("</span>");
                }
                sb.Append("</td>");
                sb.Append("<td align=\"center\"><input name=\"checkAll\" type=\"checkbox\"></td>");
                sb.Append("</tr>");
                ManagerRoleTree(list, newList[i].id, sb, roleDTO);
            }
            return sb.ToString();
        }

        public static MvcHtmlString ManagerRoleDrp(int id)
        {
            List<ManagerRole> roleList = managerRoleAppService.GetList("");
            StringBuilder sb = new StringBuilder();
            sb.Append("<option value=\"\" >请选择角色...</option>");
            for (int i = 0; i < roleList.Count; i++)
            {
                if (id == roleList[i].id)
                {
                    sb.Append("<option value=\"" + roleList[i].id + "\" selected>" + roleList[i].role_name + "</option>");
                }
                else
                {
                    sb.Append("<option value=\"" + roleList[i].id + "\">" + roleList[i].role_name + "</option>");
                }
            }
            return MvcHtmlString.Create(sb.ToString());
        }

        public static MvcHtmlString ManagerRoleType(int id)
        {

            StringBuilder sb = new StringBuilder();
            if (id == 2)
            {
                sb.Append("<option value=\"2\" selected>系统用户</option>");
            }
            else
            {
                sb.Append("<option value=\"2\">系统用户</option>");
            }
            return MvcHtmlString.Create(sb.ToString());
        }
        #endregion
        
    }
}